<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//原始单据
class Originalsaleslist extends Base
{
    public function index()
    {
        $data = $this->request->param();
        if (!isset($data['is_dj']) || $data['is_dj'] == 0) {
            $data['is_dj'] = 0;
            $dj = [0];
        }
        if ($data['is_dj'] == 1) {
            $dj = [0, 1];
        }

        if (!isset($data['order']) || $data['order'] == 0) {
            $order = 0;
        } else {
            $order = 1;
        }
        if ($order == 1) {
            $o = 'asc';
        } else {
            $o = 'desc';
        }


        $plist = Db::name('upload_img')->where('type', 1)->where('is_dj', 'in', $dj)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->distinct(true)->field('create_date')->order('create_date ' . $o)->select();
        $list = Db::name('upload_img')->where('type', 1)->where('is_dj', 'in', $dj)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->select();
        $this->assign('plist', $plist);
        $this->assign('list', $list);
        $this->assign('is_dj', $data['is_dj']);
        $this->assign('order', $order);
        return $this->fetch();
    }

    //导入附件
    public function add()
    {
        $data = $this->request->param();
        return $this->fetch();
    }

    //删除附件
    public function del()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();
        $info = Db::name('upload_img')->where('is_del', 0)->where('id', $data['id'])->find();
        if (empty($info)) {
            $msg = '参数错误';
            return ['status' => $status, 'msg' => $msg];
        } else {
            if ($info['is_dj'] == 1) {
                $msg = '已关联订单，不可删除';
                return ['status' => $status, 'msg' => $msg];
            }
            $update = [
                'is_del' => 1,
                'updatetime' => time(),
            ];
            $res = Db::name('upload_img')->where('id', $data['id'])->update($update);
            if ($res) {
                $status = 1;
                $msg = "删除成功";
            }
        }

        return ['status' => $status, 'msg' => $msg];
    }
}
